package com.tianxin.workbao.workBao.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.Toolbar;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.OutputBill;
import com.tianxin.workbao.workBao.dataProvide.OutputProvide;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;

/**
 * Created by hacker on 2017/6/22.
 */

public class BackBillEntranceActivity extends BaseActivity implements View.OnClickListener, UpdateListViewListener {

    private FloatingActionButton mFab;
    private OutputBill mOutputBill;
    private OutputProvide mProvide;
    private Button sureBtn;
    private EditText billNumberET;
    private static final String BILL_HEADER = "CKTXMY";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.initStatusBar(true);
        setContentView(R.layout.back_bill_entance);

        initView();

        initData();

        initEvent();
    }

    @Override
    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
        return super.onCreateView(parent, name, context, attrs);
    }


    public void initView() {
        mToolbar = findViewById(R.id.toolbar);
        mToolbar.setTitle(R.string.back_bill_entrance);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(AppCompatResources.getDrawable(this, R.drawable.ic_action_back_24dp));
        mFab = findViewById(R.id.list_fab);

        sureBtn = findViewById(R.id.sure_btn);
        billNumberET = findViewById(R.id.bill_number_et);
        billNumberET.setText(BILL_HEADER);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {
        mFab.setOnClickListener(view -> {
            Intent intent = new Intent(BackBillEntranceActivity.this, BackBillListActivity.class);
            startActivity(intent);
        });
        //navigationIcon event
        mToolbar.setNavigationOnClickListener(view -> BackBillEntranceActivity.this.finish());
        sureBtn.setOnClickListener(this);
    }

    @Override
    public void bindView() {

    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }

    public OutputProvide getProvide() {
        if (mProvide == null) {
            mProvide = new OutputProvide(this, this);
        }
        return mProvide;
    }


    //View.OnClickListener
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sure_btn:
                gotoInputBackbill();
                break;
        }
    }

    /**
     * get output bill and start BackBillOptionActivity
     */
    private void gotoInputBackbill() {
        String billnumber = billNumberET.getText().toString().trim();
        if (!billnumber.startsWith(BILL_HEADER)) {
            getSweetAlertDialog(this).showError("", "磅单编号必须与" + BILL_HEADER + " 开头");
            billNumberET.setText(BILL_HEADER);
            return;
        }
        if (billnumber.length() < 18) {
            getSweetAlertDialog(this).showError("", "磅单编号输入有错");
            billNumberET.setText(BILL_HEADER);
            return;
        }
        getProvide().getOutPutBillByNumber(billnumber);
    }

    @Override
    public void showMessage(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showProgress() {
        getSweetAlertDialog(this).showDialog("",0);
    }

    @Override
    public void hideProgress() {
        getSweetAlertDialog(this).dismiss();
    }

    @Override
    public void onRefreshData(Object result) {
        if (result instanceof OutputBill) {
            mOutputBill = (OutputBill) result;
            if (mOutputBill != null) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("backBill",mOutputBill);
                bundle.putInt("postition",0);
                Intent intent = new Intent(this, BackBillOptionActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        } else {
            showMessage("请求出错！");
        }
    }

    @Override
    public void onLoadMoreData(Object result) {

    }

    @Override
    protected void onRestart() {
        billNumberET.setText(BILL_HEADER);
        super.onRestart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
